package com.dqhl.smartstable.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.smartstable.R;
import com.dqhl.smartstable.adapter.AlarmLogListAdapter;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.model.AlarmLog;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 16/10/18.
 * 警报日志
 */
public class AlarmLogActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView iv_top_back;
    private TextView tv_top_center;
    private TextView tv_top_right;
    private ListView lv_alarm_log;
    private List<AlarmLog> alarmLogList = new ArrayList<>();
    private AlarmLogListAdapter alarmLogListAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_log);
        context = this;

        initView();
        initData();
    }

    private void initData() {
        RequestParams params = new RequestParams(Config.Api.alarm_log);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    alarmLogList = JSON.parseArray(data, AlarmLog.class);
                    alarmLogListAdapter = new AlarmLogListAdapter(context, alarmLogList);
                    lv_alarm_log.setAdapter(alarmLogListAdapter);
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("警报日志");
        tv_top_right = (TextView) findViewById(R.id.tv_top_right);
        tv_top_right.setText("清除");

        lv_alarm_log = (ListView) findViewById(R.id.lv_alarm_log);

        iv_top_back.setOnClickListener(this);
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_top_right: //清除
                clearLog();
                break;
        }
    }

    /**
     * 清除
     */
    private void clearLog() {
        // TODO: 16/10/18
    }
}
